package com.suse.lhw.hms.foodserver.controller;

import com.netflix.client.http.HttpResponse;
import com.netflix.discovery.converters.Auto;
import com.suse.lhw.hms.foodserver.constant.FoodModelStatusConst;
import com.suse.lhw.hms.foodserver.dao.FPackageMapper;
import com.suse.lhw.hms.foodserver.domin.*;
import com.suse.lhw.hms.foodserver.pojo.input_POJO.addAFoodModel;
import com.suse.lhw.hms.foodserver.pojo.input_POJO.addAFoodModel_unit;
import com.suse.lhw.hms.foodserver.pojo.input_POJO.modelAndUnits;
import com.suse.lhw.hms.foodserver.pojo.output_POJO.ofm_selectFoodIfoByModelId;
import com.suse.lhw.hms.foodserver.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.object.UpdatableSqlQuery;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.management.RuntimeErrorException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.AnnotatedArrayType;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.LoggingPermission;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.foodserver.com.suse.lhw.security.controller
 * @ClassName: FoodManagerController
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/4/24 9:53
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/24 9:53
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@RestController
@RequestMapping("foodmanager")
public class FoodManagerController {
    @Autowired
    FFoodmodelService fFoodmodelService;
    @Autowired
    FUnitService fFoodmodelUnitService;
    @Autowired
    FFoodmodelToFFoodmodelUnitService fFoodmodelToFFoodmodelUnitService;
    @Autowired
    FFoodIfoService fFoodIfoService;
    @Autowired
    FPackageService packageService;
    @Autowired
    FPackageToFFoodmodelService packageToFFoodmodelService;

    @PostMapping("addAFoodModel")//增加四个字段，Foodmodel,f_foodmodel_to_f_foodmodel_unit,f_unit,f_food_ifo
    public String addAFoodModel(Model model, addAFoodModel pojo, @RequestBody List<FUnit> list) {
        try {
            String result = fFoodmodelService.addAFoodModel(pojo, list);
            return result;
        } catch (RuntimeException e) {
            e.printStackTrace();
//            throw new RuntimeException();
            return "网络繁";
        }
    }

    @PostMapping("deleteADefautFoodModel")
    public String deleteADefaultFoodModel(Integer foodmodelId) {
        /**
         * @Author:Hongwei Li
         * @Description:删除默认的食品模型，顺便删除食品信息，食品单位及其映射
         * @Date:11:17 2020/4/26
         * @Param:[foodmodelId]
         * @Return:java.lang.String
         **/

        try {
            String result = fFoodmodelService.deleteADefaultFoodModel(foodmodelId);
            return result;
        } catch (RuntimeErrorException e) {
            e.printStackTrace();
//            throw new RuntimeException();
            return "网络繁忙！";
        }

    }

    @GetMapping("findAllFoodModelsOfDefault")
    public void findAllFoodModelsOfDefault(HttpResponse response, HttpServletRequest request, Model model) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:查找所有的默认食品模型
         * @Date:11:19 2020/4/26
         * @Param:[model]
         * @Return:void
         **/
        try {
            List<FFoodmodel> foodmodelList = fFoodmodelService.selectFFoodmodelsByStatus(FoodModelStatusConst.defaut);
            if (foodmodelList.isEmpty()) {
                System.out.println("没有搜到哦");
            }
            for (FFoodmodel fFoodmodel : foodmodelList) {
                System.out.println(fFoodmodel.toString());
            }
            model.addAttribute("foodmodeList", foodmodelList);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("selectFoodIfoByModelId")
    public ofm_selectFoodIfoByModelId selectFoodIfoById(int modelId, Model model) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:通过用户点击食品模型传来的id来查找对应的信息以及其单位
         * @Date:14:18 2020/4/26
         * @Param:[modelId]
         * @Return:void
         **/
        try {
            FFoodIfo ifo = fFoodIfoService.selectFoolIfoByModelId(modelId);
            model.addAttribute("ifo", ifo);
            List<FUnit> unitList = fFoodmodelUnitService.selectAllUnitByModelId(modelId);
            System.out.println(ifo.toString());
            return new ofm_selectFoodIfoByModelId(ifo.getDescroption(), ifo.getCarbohydrate(), ifo.getProtein(), ifo.getFat(), ifo.getTag(), ifo.getOrtherNutrient(), unitList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("updateAFoodModel")
    public String updateAfoodModel(FFoodmodel fFoodmodel) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:更新食品模型
         * @Date:16:52 2020/4/26
         * @Param:[fFoodmodel]
         * @Return:java.lang.String
         **/
        fFoodmodel.setSatus(FoodModelStatusConst.defaut);
        System.out.println("foodmodel是：" + fFoodmodel.toString());
        int result = fFoodmodelService.updateByPrimaryKeySelective(fFoodmodel);
        if (result == 0) {
            System.out.println("更新失败");
            return "更新失败了，好好反省一下自己";
        }
        System.out.println("跟新成功，成功的");
        return "更新成功！";
    }

    @PostMapping("updateFoodUnits")
    public String updateFoodUnits(@RequestBody List<FUnit> list) {
        /**
         * @Author:Hongwei Li
         * @Description:更新食品食物单位
         * @Date:19:47 2020/4/26
         * @Param:[list]
         * @Return:java.lang.String
         **/
        for (FUnit fUnit : list) {
            if (fFoodmodelUnitService.updateByPrimaryKeySelective(fUnit) == 0) {
                System.out.println("更新失败@！反省");
            }
        }
        return "更新成功！";
    }

    @PostMapping("addADefaultPackage")
    public String addADefaultPackage(FPackage fPackage, @RequestBody List<FPackageToFFoodmodel> list) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:创建一个默认的食品套餐，顺便创建套餐对食品模型的映射
         * @Date:21:32 2020/4/26
         * @Param:[fPackage, list]
         * @Return:java.lang.String
         **/
        if (packageService.addADefaultPackage(fPackage, list) == 0)
            return null;
        else return "创建成功!";
    }

    @PostMapping("deleteADefaultPackageById")
    public String deleteADefaultPackageById(int id) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:删除食品套餐与食品模型的映射
         * @Date:21:53 2020/4/26
         * @Param:[id]
         * @Return:java.lang.String
         **/
        if (packageService.deleteADefaultPackageByPrimaryKey(id) == 0) {
            System.out.println("删除套餐失败");
            return "删除失败！";
        }
        packageService.deleteByPrimaryKey(id);
        return ";";
    }

    @GetMapping("selectAllDefaultPackage")
    public List<FPackage> selectAllDefaultPackage() {
        /**未测试
         * @Author:Hongwei Li
         * @Description:查找所有的套餐
         * @Date:21:59 2020/4/26
         * @Param:[]
         * @Return:java.util.List<com.suse.lhw.hms.foodserver.domin.FPackage>
         **/
        List<FPackage> list = packageService.selectDefualtPackages();
        return list;
    }

    @GetMapping("selectAllModelByPackageId")
    public List<ofm_selectFoodIfoByModelId> selectAllModelByPackageId(int packageId) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:通过套餐的id查找出所有对应的食品模型和单位，组合成为一个POJO
         * @Date:22:14 2020/4/26
         * @Param:[packageId]
         * @Return:java.util.List<com.suse.lhw.hms.foodserver.pojo.output_POJO.ofm_selectFoodIfoByModelId>
         **/
        return fFoodmodelService.selectAllModelByPackageId(packageId);
    }

    @PostMapping("updateAPackageByPackageId")
    public String updateAPackageByPackageId(FPackage fPackage) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:只更新fpackage的信息
         * @Date:9:14 2020/4/27
         * @Param:[fPackage]
         * @Return:java.lang.String
         **/
        if (packageService.updateByPrimaryKeySelective(fPackage) == 0) {
            return "更新套餐失败！";
        }
        return "更新套餐成功";
    }

    @PostMapping("addAModelOfPackage")
    public String addAModelOfPackage(int packageId, int modelId) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:为套餐增加一个食品模型
         * @Date:10:15 2020/4/27
         * @Param:[packageId, modelAndUnit]
         * @Return:java.lang.String
         **/
        if (packageToFFoodmodelService.addAModelOfPackage(modelId, packageId) == 0) {
            System.out.println("添加套餐的食品模型及单位失败！");
            return "添加套餐的食品模型及单位失败";
        } else {
            return "添加成功！";
        }
    }

    @PostMapping("deleteAModelOfPackage")
    public String deleteAModelOfPakcage(int packageId, int modelId) {
        /**未测试
         * @Author:Hongwei Li
         * @Description:删除套装的模型
         * @Date:9:06 2020/4/29
         * @Param:[packageId, modelId]
         * @Return:java.lang.String
         **/
        if (packageToFFoodmodelService.deleteAModelOfPackage(packageId, modelId) == 0) {
            System.out.println("删除失败！");
            return "删除失败！";
        }
        return "删除成功@！";
    }


}
